package com.nbsaas.boot.controller.shopping;

import com.nbsaas.boot.rest.annotations.*;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.rest.response.PageResponse;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.shopping.api.domain.request.ShoppingCartRequest;
import com.nbsaas.boot.shopping.api.domain.request.ShoppingCartSearch;
import com.nbsaas.boot.shopping.api.domain.response.ShoppingCartResponse;
import com.nbsaas.boot.shopping.api.domain.simple.ShoppingCartSimple;
import com.nbsaas.boot.shopping.api.apis.ShoppingCartApi;
import com.nbsaas.boot.shopping.ext.apis.ShoppingCartCheckoutApi;
import com.nbsaas.boot.shopping.ext.apis.ShoppingCartExtApi;
import com.nbsaas.boot.shopping.ext.apis.ShoppingCartSubmitApi;
import com.nbsaas.boot.shopping.ext.domain.request.ShoppingCartCheckedRequest;
import com.nbsaas.boot.shopping.ext.domain.request.ShoppingCartCheckoutRequest;
import com.nbsaas.boot.shopping.ext.domain.request.ShoppingCartSubmitRequest;
import com.nbsaas.boot.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import javax.annotation.Resource;

/**
*  购物车控制器
*/
@RequiresAuthentication
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {


    @Resource
    private ShoppingCartApi shoppingCartApi;




    @Resource
    private ShoppingCartExtApi shoppingCartExtApi;



    @Resource
    private ShoppingCartCheckoutApi shoppingCartCheckoutApi;



    @Resource
    private ShoppingCartSubmitApi shoppingCartSubmitApi;

    @SearchData
    @PostMapping("/submit")
    public ResponseObject<?> submit(@RequestBody ShoppingCartSubmitRequest request) {
        request.setCreator(UserUtils.user().getId());
        return shoppingCartSubmitApi.submit(request);
    }

    @SearchData
    @PostMapping("/checkout")
    public ResponseObject<?> checkout(@RequestBody ShoppingCartCheckoutRequest request) {
        request.setCreator(UserUtils.user().getId());
        return shoppingCartCheckoutApi.checkout(request);
    }

    @SearchData
    @PostMapping("/checked")
    public ResponseObject<?> checked(@Validated@RequestBody ShoppingCartCheckedRequest request) {
        return shoppingCartExtApi.checked(request);
    }

    @CreateData
    @PostMapping("/createExt")
    public ResponseObject<?> createExt(@Validated@RequestBody ShoppingCartRequest request) {
        request.setCreator(UserUtils.user().getId());
        return shoppingCartExtApi.createExt(request);
    }

    /**
    * 搜索数据
    *
    * @param request
    * @return
    */
    @SearchData
    @PostMapping("/search")
    public PageResponse <ShoppingCartSimple> search(@RequestBody ShoppingCartSearch request) {
        request.setCreator(UserUtils.user().getId());
        return shoppingCartApi.search(request);
    }

    /**
    * 列表数据
    *
    * @param request
    * @return
    */
    @SearchData
    @PostMapping("/list")
    public ListResponse<ShoppingCartSimple> list(@RequestBody ShoppingCartSearch request) {
        request.setCreator(UserUtils.user().getId());
        return shoppingCartApi.list(request);
    }

    /**
    * 添加数据
    *
    * @param request
    * @return
    */
    @CreateData
    @PostMapping("/create")
    public ResponseObject <ShoppingCartResponse> create(@RequestBody @Validated(AddOperator.class) ShoppingCartRequest request) {
        request.setCreator(UserUtils.user().getId());
        return shoppingCartApi.create(request);
    }

   /**
   * 修改数据
   * @param request
   * @return
   */
   @UpdateData
   @PostMapping("/update")
   public ResponseObject<ShoppingCartResponse> update(@RequestBody @Validated(UpdateOperator.class) ShoppingCartRequest request) {
       request.setCreator(UserUtils.user().getId());
       return shoppingCartApi.update(request);
   }

    /**
    * 删除数据
    *
    * @param request
    * @return
    */
    @SearchData
    @PostMapping("/delete")
    public ResponseObject<?> delete(@RequestBody @Validated(DeleteOperator.class) ShoppingCartRequest request) {
        request.setCreator(UserUtils.user().getId());
        return shoppingCartApi.delete(request);
    }

    /**
    * 查看数据
    *
    * @param request
    * @return
    */
    @SearchData
    @PostMapping("/view")
    public ResponseObject <ShoppingCartResponse> view(@RequestBody @Validated(ViewOperator.class) ShoppingCartRequest  request) {
        request.setCreator(UserUtils.user().getId());
        return shoppingCartApi.view(request);
    }
}